﻿using General.Framework;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace General.Mvc.Filters
{
    public class AdminCheckFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //base.OnActionExecuting(filterContext);
            //效验用户是否已经登录
            var adminSession = filterContext.HttpContext.Session.GetString("Admin");
            if (string.IsNullOrEmpty(adminSession))
            {
                if (filterContext.Controller.ToString().Contains("AdminApi"))
                {
                    ContentResult content = new ContentResult() { StatusCode = 200, Content = JsonHelper.Serialize(new AjaxResult() { code= AjaxResultStateEnum.Error,message ="请先登录！" }) };
                    filterContext.Result = content;
                }
                else
                {
                    //filterContext.HttpContext.Response.Redirect("/Admin/Login");
                    RedirectToActionResult content = new RedirectToActionResult("Login", "Admin", null);
                    filterContext.Result = content;
                }
                
            }
            else
            {
                //滑动过期
                filterContext.HttpContext.Session.SetString("Admin", adminSession);
            }
            

            //return;
        }
    }
}
